import lib_car from "../../../api/lib_car"
import _city from "../../../cache/_city"
import _layer from "../../../common/_layer";
import _object from "../../../utils/_object";
import _activity from "../../../cache/_activity";
import _series from "../../../cache/_series"
import _multiple_ui from "../../../cache/_multiple_ui";

Page({
  data: {
    activity_id: '',
    tids: 0,
    clickid: '',

    series: '',
    series_id: 0,
    product_list: [],
    product_name: '',
    year: '',
    product_id: 0,
    city: '',

    is_loading: false,
    is_empty: false,
    type_list: ['全款', '分期'],
    currindex: 0,

    full_payment_total_price: 0, //全款总价 = 裸车价格 + 必要花费 + 商业保险
    city_refer_price: 0, //裸车价格

    need_price: 0, //必要花费
    optax_price: 0, //购置税 = 裸车价/11.7
    compulsory_insurance_price: 0, //交强险
    use_tax_price: 0, //车船使用税
    is_vehicle_and_vessel_use_tax: true, //是否有车船使用税
    licensing_fee_price: 0, //上牌税

    business_insurance_price: 0, //商业险 (全险) = 裸车价*0.011

    total_installment_price: 0, //分期总价 = 首付30% + 月供 + 商业险 + 必要花费
    total_installment_cost: 0, //分期总花费
    down_payments: 0, //首付  按36期计算，裸车价格*0.3
    monthly_supply: 0, //月供  (裸车价格 - 首付)/36 + (裸车价格 - 首付)*0.03/12


    // 单位 （元/万）

    full_payment_total_price_unit: '', //全款总价
    city_refer_price_unit: '', //裸车价格

    need_price_unit: '', //必要花费
    optax_price_unit: '', //购置税 
    compulsory_insurance_price_unit: '', //交强险
    use_tax_price_unit: '', //车船使用税
    licensing_fee_price_unit: '', //上牌税

    business_insurance_price_unit: '', //商业险

    total_installment_price_unit: '', //分期总价
    total_installment_cost_unit: '', //分期总花费
    down_payments_unit: '', //首付 
    monthly_supply_unit: '', //月供  

    is_multiple: false,


  },
  onLoad: function (options) {
    if (_object._length(options) > 0 && options.hasOwnProperty('product_name')) {
      this.setData({
        product_name: options.product_name
      })
    }

    if (_object._length(options) > 0 && options.hasOwnProperty('product_id')) {
      this.setData({
        product_id: options.product_id
      })
    }

  },

  onShow() {

    this.setData({
      city: _city.get(),
    })

    if (_activity.get() && _object._length(_activity.get()) > 0) {
      this.setData({
        activity_id: _activity.get().activity_id,
        tids: _activity.get().tids,
        clickid: _activity.get().clickid,
      })
    }
    //UI
    let multiple_ui = _multiple_ui.get();

    if (multiple_ui && multiple_ui.is_multiple == 1) {
      this.setData({
        is_multiple: true,
      })
    } else {
      this.setData({
        is_multiple: false
      })
    }

    //车型
    let cach_series = _series.get()
    if (cach_series && _object._length(cach_series) > 0) {
      this.setData({
        series: cach_series.series_name,
        series_id: cach_series.series_id
      })
    }

    this.init_product()

  },

  //车款
  init_product() {
    lib_car.productyc(this.data.series_id, this.data.series).then((res) => {
      if (res.code != 0) {
        _layer.message(res.msg);
        return;
      }

      if (res.data.length == 0) {
        this.setData({
          is_empty: true
        })
        //没有车款更无价格
        return;
      }

      this.setData({
        product_list: res.data,
      })
      // 初次 切换页面车款
      this.setData({
        product_name: this.data.product_name ? this.data.product_name : (res.data[0].year + '款 ' + res.data[0].product_name),
        product_id: this.data.product_id ? this.data.product_id : res.data[0].id
      })

      this.init_price(this.data.product_id, this.data.city);

    })
  },

  // 价格
  init_price(product_id, city_name) {
    // console.log(product_id, city_name, '价格入参');
    this.setData({
      is_loading: true,
    })
    let data;
    lib_car.productycp(product_id, city_name).then((res) => {
      if (res.code != 0) {
        _layer.message(res.msg);
        return;
      }

      data = res.data;
      this.setData({
        is_loading: false,
      })
      if (data == null) {
        this.setData({
          is_empty: true
        })
        return;
      }
      // console.log(data, '价格价格');
      this.convert_fn(data)
      this.setData({
        is_empty: false
      })

    })
  },
  // 全款分期切换
  change_tag(e) {
    const { index } = e.currentTarget.dataset
    this.setData({ currindex: index })

  },
  // 切换车型
  change_product() {

    if (this.data.product_list.length <= 1) {
      _layer.message('暂无其他车款')
      return;
    }

    // console.log(this.data.product_id, this.data.product_name, '切换车款');
    tt.redirectTo({
      url: `/packageMy/pages/model/model?product_name=${this.data.product_name}&product_id=${this.data.product_id}`,
    });
  },


  //分享
  async onShareAppMessage(option) {

    const { activity_id, tids, clickid } = this.data
    // console.log(this.data.is_multiple, '是否为多品');

     //单品跟多品的分享  单品--list页面  多品-- 报名页

    if (this.data.is_multiple == true) {
      return {
        title: '查询底价',
        path: `/pages/multiple/common/common?activity_id=${activity_id}&tids=${tids}&clickid=${clickid}`, // ?后面的参数会在转发页面打开时传入onLoad方法
        imageUrl: '/assets/image/multiple_banner.jpg', // 支持本地或远程图片，默认是小程序 icon
        success(res) {
          console.log('分享成功', res);
        },
        fail() {
          console.log("转发发布器调起失败");
        },
      };

    } else {
      return {
        title: '查询底价',
        path: `/pages/single/list/list?activity_id=${activity_id}&tids=${tids}&clickid=${clickid}`, // ?后面的参数会在转发页面打开时传入onLoad方法
        imageUrl: '/assets/image/banner.png', // 支持本地或远程图片，默认是小程序 icon
        success(res) {
          console.log('分享成功', res);
        },
        fail() {
          console.log("转发发布器调起失败");
        },
      };
    }

  },

  //  格式转换
  formatAmountInWan(n) {
    if (Math.ceil(n) >= 10000) {
      return (n / 10000).toFixed(2)
    } else {
      return Math.ceil(n)
    }
  },
  //判断是元还是万
  is_yuan_wan(n) {

    if (Math.ceil(n) >= 10000) {
      return '万'
    } else {
      return '元'
    }
  },

  convert_fn(data) {
    // console.log(data, '价格价格');
    //首付
    let new_down_payments = data.city_refer_price * 0.3;
    //裸车 - 首付
    let residue_money = data.city_refer_price - new_down_payments
    //月供
    let new_monthly_supply = (residue_money / 36) + ((residue_money * 0.03) / 12)
    //分期总价
    let new_total_installment_price = new_down_payments + data.need_price + data.business_insurance_price;
    //分期总花费
    let new_total_installment_cost = new_total_installment_price + new_monthly_supply * 36;


    this.setData({
      city_refer_price: this.formatAmountInWan(data.city_refer_price),//裸车价格
      need_price: this.formatAmountInWan(data.need_price),//必要花费
      optax_price: data.optax_price ? this.formatAmountInWan(data.optax_price) : this.formatAmountInWan(data.city_refer_price / 11.7), //购置税
      compulsory_insurance_price: this.formatAmountInWan(data.compulsory_insurance_price), // 交强险
      use_tax_price: this.formatAmountInWan(data.use_tax_price),//车船使用税
      licensing_fee_price: this.formatAmountInWan(data.licensing_fee_price),//上牌税
      business_insurance_price: data.business_insurance_price ? this.formatAmountInWan(data.business_insurance_price) : this.formatAmountInWan(data.city_refer_price * 0.011), //商业险
      full_payment_total_price: this.formatAmountInWan(data.city_refer_price + data.need_price + data.business_insurance_price), //全款
      down_payments: this.formatAmountInWan(new_down_payments), //首付 = 裸车价格*0.03
      monthly_supply: this.formatAmountInWan(new_monthly_supply), // (裸车价格 - 首付)/36 + (裸车价格 - 首付)*0.03/12
      total_installment_price: this.formatAmountInWan(new_total_installment_price),// 分期总价
      total_installment_cost: this.formatAmountInWan(new_total_installment_cost), //分期总花费

      // 元 万
      full_payment_total_price_unit: this.is_yuan_wan(data.city_refer_price + data.need_price + data.business_insurance_price),

      city_refer_price_unit: this.is_yuan_wan(data.city_refer_price), //裸车价格

      need_price_unit: this.is_yuan_wan(data.need_price), //必要花费
      optax_price_unit: this.is_yuan_wan(data.optax_price), //购置税 
      compulsory_insurance_price_unit: this.is_yuan_wan(data.compulsory_insurance_price), //交强险
      use_tax_price_unit: this.is_yuan_wan(data.use_tax_price), //车船使用税
      licensing_fee_price_unit: this.is_yuan_wan(data.licensing_fee_price), //上牌税

      business_insurance_price_unit: this.is_yuan_wan(data.business_insurance_price), //商业险


      down_payments_unit: this.is_yuan_wan(new_down_payments), //首付 
      monthly_supply_unit: this.is_yuan_wan(new_monthly_supply), //月供  

      total_installment_price_unit: this.is_yuan_wan(new_total_installment_price), //分期总价
      total_installment_cost_unit: this.is_yuan_wan(new_total_installment_cost), //分期总花费

    })
  }
})